﻿using Coravel;
using Coravel.Invocable;

namespace SampleWeb;

public static class AppConfig
{
    public const string Branch = "Known";
    public const string SubTitle = "基于Blazor的企业级快速开发框架";
    public static string AppId => "KIMS";
    public static string AppName => "Known信息管理系统";

    public static void AddApplication(this IServiceCollection services, Action<AppInfo> action = null)
    {
        //Stopwatcher.Enabled = true;
        Console.WriteLine(AppName);
        var assembly = typeof(AppConfig).Assembly;
        services.AddKnown(info =>
        {
            //项目ID、名称、类型、程序集
            info.Id = AppId;
            info.Name = AppName;
            info.IsSize = true;
            info.IsLanguage = true;
            info.IsTheme = true;
            info.Assembly = assembly;
        });
        services.AddKnownCore(action);
        services.AddKnownAdmin();
        services.AddKnownAdminCore();
        services.AddKnownCells();
        services.AddKnownWeb();

        services.AddScoped<IHomeService, HomeService>();

        services.AddScheduler();
        services.AddTransient<ImportTaskJob>();

        UIConfig.Errors["403"] = new ErrorConfigInfo { Description = "你没有此页面的访问权限。" };
    }

    public static void UseApplication(this WebApplication app)
    {
        //使用Known框架
        app.UseKnown();
        //配置定时任务
        app.Services.UseScheduler(scheduler =>
        {
            //每5秒执行一次异步导入
            //scheduler.Schedule<ImportTaskJob>().EveryFiveSeconds();
        });
    }
}

class ImportTaskJob : IInvocable
{
    public Task Invoke() => ImportHelper.ExecuteAsync();
}